package com.example.myproject.redis.redisother.fangan2;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

/**
 * Created by xi on 2019/3/2.
 */
@Component
public class ReceiverListener implements MessageListener {

    public void handleMessage(Object message) {
        System.out.println("handleMessage:"+message);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {

        System.out.println("onMesssagegewewe");

        ObjectMapper objectMapper = new ObjectMapper();
        //忽略传入到对象的多余字段的解析
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //解析的时候自动映射成类型
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        //objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        RedisSerializer serializer = new GenericJackson2JsonRedisSerializer(objectMapper);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //接收的topic
        String channel = stringRedisSerializer.deserialize(message.getChannel());
        //消息的POJO
        Object object = serializer.deserialize(message.getBody());
        System.out.println("ReceiverListener232:" + message + ",class====>:" + object.getClass());

    }
}